package com.mxpio.erp.sales.controller;


import com.mxpio.erp.common.sales.entity.ShippingNotice;
import com.mxpio.erp.common.sales.entity.ShippingNoticeLine;
import com.mxpio.erp.common.sales.service.ShippingNoticeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(value = "ShippingNoticeController", tags = { "发货通知单接口" })
@RestController
@RequestMapping("/erp/sales/sn/")
public class ShippingNoticeController {

    @Autowired
    private ShippingNoticeService service;

    @GetMapping("list")
    @ApiOperation(value = "发货通知单列表", notes = "获取发货通知单列表（无分页）", httpMethod = "GET")
    public Result<List<ShippingNotice>> list(@ApiParam(name="查询参数")  String criteria) throws UnsupportedEncodingException {
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        List<ShippingNotice> shippingNotice = service.list(ShippingNotice.class, c);
        return Result.OK(shippingNotice);
    }

    @GetMapping("page")
    @ApiOperation(value = "发货通知单列表", notes = "获取发货通知单列表（分页）", httpMethod = "GET")
    public Result<Page<ShippingNotice>> page(@ApiParam(name="查询参数")String criteria, @ApiParam(name="每页条数")Integer pageSize, @ApiParam(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        Page<ShippingNotice> shippingNotice = service.listPage(ShippingNotice.class, page, c);
        return Result.OK(shippingNotice);
    }

    @GetMapping("list/{noticeNo}")
    @ApiOperation(value = "根据noticeNo获取发货通知单", notes = "根据noticeNo获取发货通知单", httpMethod = "GET")
    public Result<ShippingNotice> getById(@ApiParam(name="业务单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
        ShippingNotice shippingNotice = service.getById(ShippingNotice.class, noticeNo);
        return Result.OK(shippingNotice);
    }

    @PostMapping("save")
    @ApiOperation(value = "保存发货通知单", notes = "保存发货通知单", httpMethod = "POST")
    public Result<ShippingNotice> save(@RequestBody ShippingNotice notice) {
        service.save(notice,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if(o instanceof ShippingNoticeLine){
                    ShippingNotice shippingNotice = context.getParent();
                    ((ShippingNoticeLine) o).setNoticeNo(shippingNotice.getNoticeNo());
                }
                return true;
            }
        });
        return Result.OK(notice);
    }


    @DeleteMapping("remove/{noticeNos}")
    @ApiOperation(value = "根据noticeNo删除发货通知单", notes = "删除发货通知单", httpMethod = "DELETE")
    public Result<ShippingNotice> remove(@ApiParam(name="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "noticeNos", required = true) String noticeNos) {
        String[] noticeNo = noticeNos.split(",");
        for(String key : noticeNo){
            service.delete(ShippingNotice.class, key);
        }
        return Result.OK();
    }

}
